package com.tang.model.proxy.dynamicproxy.jdkproxy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/8/17 6:31
 * @Desc 测试类
 * @Remarks
 */
public class DynamicProxyTest {
    public static void main(String[] args) {
        MatchmakerProxy workerProxy = new MatchmakerProxy();
        IPerson instance = (IPerson) workerProxy.getInstance(new GenericWorker());

//        byte[] bytes = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{IPerson.class});
//        writeToFile(bytes, "D:\\gitee\\design_model\\proxy_model\\target\\classes\\com\\tang\\model\\proxy\\dynamicproxy\\jdkproxy\\", "Tang.class");
        instance.findLove();
        System.out.println("========================");
        MatchmakerProxy programmerProxy = new MatchmakerProxy();
        IPerson programmer = (IPerson) programmerProxy.getInstance(new Programmer());
        programmer.findLove();
        System.out.println("========================");
        instance.findLove();


    }

    private static void writeToFile(byte[] bytes, String packageName, String fileName) {
        File file = new File(packageName + "\\" + fileName);
        FileOutputStream fileOutputStream = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
            }
        }
    }
}
